<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{ row }">
              {{ formatEmployment(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="账户状态" />
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[10, 15, 20, 30]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog title="新增员工" :visible.sync="showDialog" @close="hCloseDialog">
        <addor-edit ref="AddEmployee" @close="showDialog=false" @closeUpdate="closeUpdate" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import EmployeeEnum from '@/constant/employees'
const hireType = EmployeeEnum.hireType.reduce((acc, item) => {
  acc[item.id] = item.value
  return acc
}, {})
import { delEmployee, getEmployeeList } from '@/api/employees'
import addorEdit from './empDialog.vue'
export default {
  components: {
    addorEdit
  },
  data() {
    return {
      value: true,
      list: [],
      page: 1, // 当前页码
      size: 10, // 每页几条
      total: 0, // 总共数据条数
      showDialog: false
    }
  },
  created() {
    this.loadEmployeeList()
  },
  methods: {
    // 清空表单
    hCloseDialog() {
      this.$refs.AddEmployee.resetForm()
    },
    closeUpdate() {
      this.showDialog = false
      this.total++
      // 最后一页
      this.page = Math.ceil(this.total / this.size)
      this.loadEmployeeList()
    },
    async loadEmployeeList() {
      try {
        const res = await getEmployeeList(this.page, this.size)
        console.log(res, '员工列表')
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
        this.$message.error('获取员工数据失败')
      }
    },
    formatEmployment(data) {
      // console.log('执行一次formatEmployment')
      // // 对后端返回的类型的编码进行翻译 ，还原成 中文
      // const rs = EmployeeEnum.hireType.find(item => item.id === id)
      // return rs.value
      return hireType[data] || '未知'
    },
    // 会自动接收当前点击的页码
    handleCurrentChange(curPage) {
      // alert(curPage)
      // 1. 更新页码
      this.page = curPage
      // 2. 重发请求
      this.loadEmployeeList()
    },
    // 每页几条
    handleSizeChange(curSize) {
      // alert(size)
      // 1. 更新每页的条数
      this.size = curSize
      // 2. 重发请求
      this.loadEmployeeList()
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该员工数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch((error) => {
        console.log(error)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async doDel(id) {
      try {
        const res = await delEmployee(id)
        console.log(res)
        this.$message.success('删除成功')
        if (this.list.length === 1) {
          this.page--
          if (this.page <= 0) {
            this.page = 1
          }
        }
        this.loadEmployeeList()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
